import requests
import json
import allure

token = ['']
@allure.step("发送请求")
def sendRequest(data):
    reqType = data['case_method'] #请求类型
    reqHeader = json.loads(data['case_headers'], strict=False) #请求头
    reqUrl = data['case_url'] #请求连接
    reqBody = json.loads(data['case_body']) #请求参数

    if reqHeader.get('token', -1) != -1:
        reqHeader['token'] = token[0]
        print("token:", reqHeader['token'])

    if reqType == 'POST-ROW':
        print("post:", data['case_description'])
        print("请求参数：",reqBody)
        resp = requests.post(reqUrl,json = reqBody,headers = reqHeader)
    else:
        print("get")
        resp = requests.get(reqUrl,json = reqBody,header = reqHeader)
    respData = resp.json()
    # 是否保存token
    if respData['code'] == '0000' and respData.get('data', -1) != 1 and respData['data'] != None and respData['data'].get('token', -1) != -1 and respData['data'].get(
            'token') != '':
        token[0] = respData['data']['token']

    return respData
    # if resp.json()['result'] == 1:
    #     print("success")
    # else:
    #     print("fails:", resp.json()['msg'])